<?php
//
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'get_detail';
if($_RQ['op']=='get_detail'){
	$return=$this->model('page/goods_detail')->getmain($return);
	$keywords=$return['goods']['share_keywords']?:$return['goods']['title'].','.$return['sharedata']['desc'];
	$desc=$return['sharedata']['desc'];
	set_html('title',$return['goods']['title']?$return['goods']['title'].'-'.$return['shopset']['title']:'商品详情');
	set_html('keywords',$keywords);
	set_html('description',$desc);
	exi($return);
}
if($_RQ['op']=='addtocart'){
	if(!$_SESSION['uid']){
		$return['ischecklogin']=$this->model('member')->checklogin();
	}
	$post=get_postdata();
	$id=intval($post['goodsid']);
	$number=intval($post['num']);
	$isjson=intval($post['isjson']);
    $isres=intval($_RQ['isres']);
	$pluginname=$_RQ['pluginname']?:'';
	if(!$id){
		exi('选择的商品不存在，请重新选择！','error');
	}
	$this->model('activity/sales')->check_sale_new(array(array('goodsid'=>$id)));
	if(!$number){
		$number=1;
	}
	$spsids = '';
	$post['optionid']=$post['optionid']?:$post['optionsid'];
	if($post['specid']){
		foreach($post['specid'] as $k=>&$s){
			if($k==0){
				$spsids .= $s;
			}else{
				$spsids .= '_'.$s;
			}
		}
		$optionid = pdo_getcolumn('jy_weishop_goods_option',array('goodsid'=>$id,'specs'=>$spsids),'id');
	}
	if(!$optionid && $post['optionid']){
		$optionid=$post['optionid'];
	}
	$goods=pdo_get('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	if(!$options['id'] && $optionid){
		$options = pdo_get('jy_weishop_goods_option',array('goodsid'=>$id,'id'=>$optionid));
	}
	$ifgoods=pdo_get('jy_weishop_member_cart',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'goodsid'=>$id,'optionid'=>$optionid));
	$data=array(
		'uniacid'=>$_SESSION['uniacid'],
		'mid'=>$_SESSION['uid'],
		'goodsid'=>$id,
		'price'=>$options['price']>0?$options['price']:$goods['price'],
		'optionid'=>$optionid,
		'selected'=>1,
	);
	if($return['shopset']['no_showmerch']==1 && $goods['merchid']){
		exi('该商品，属于商户商品，请前往商户小店购买！','error');
	}
	$goods['api_data']=json_decode($goods['api_data'],true);
	if($goods['api_data']['no_paytype']){
		$no_paytype=str_array($goods['api_data']['no_paytype']);
		if($no_paytype){
			foreach($no_paytype as $nkey=>$nv){
				if(intval($nv)){
					unset($no_paytype[$nkey]);
				}
			}
			if($no_paytype && !$goods['api_data']['no_paytypecart']){
				exi('该商品启用了[不支持的支付方式]，不能加入购物车！','error');
			}
		}
	}
	if($ifgoods){
		$cartid=$ifgoods['id'];
		$data['total']=$ifgoods['total']+$number;
		$this->model('goods')->check_buy($data['goodsid'],intval($data['total']),intval($data['optionid']),true,true);
		pdo_update('jy_weishop_member_cart',$data,array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid'],'id'=>$ifgoods['id']));
		if($post['getlist']){
		}elseif($isjson){
			exi('success');
		}else{
			exi('成功加入购物车！');
		}
	}else{
		$data['total']=$number;
		$this->model('goods')->check_buy($data['goodsid'],intval($data['total']),intval($data['optionid']),true,true);
		//如果来自其他商户分销（保存商户ID）
		$data['merchid']=$goods['merchid']?:$this->session('frommerchid');
		$data['pluginname']=$post['pluginname']?:$pluginname;
		pdo_insert('jy_weishop_member_cart',$data);
	}
	if($isjson){
		exi('success');
	}elseif($isres){
		$params=array('mid'=> $_SESSION['uid'], 'uniacid' => $_SESSION['uniacid']);
		if($pluginname){
			$params['pluginname']=$pluginname;
		}
        $res['cartnums']=pdo_getsum('jy_weishop_member_cart',$params,'total');
        exi($res);
    }else{
		exi('成功加入购物车！');
	}
}
if($_RQ['op']=='get_optionsid'){
	$post=get_postdata();
	$id=$post['goodsid']?:$_SESSION['goodsid'];
	$store_id=$_SESSION['store_id'];
	if(!$id){
		exi('请先选择商品！','error');
	}
	if($post['storeid']){
		$_SESSION['store_id']=$post['storeid'];
	}
	//规格
	$specids=explode('_',$post['specid']);
	
	//已选规格
	$selespename = '';
	if($specids){
		foreach($specids as $k=>$sp){
			$spec_item=pdo_get('jy_weishop_goods_spec_item',array('id'=>$sp,'uniacid'=>$_SESSION['uniacid']),array('id','title'));
			$selespename .=$spec_item['title'].'，';
		}
	}
	$optionsid=pdo_getcolumn('jy_weishop_goods_option',array('goodsid'=>$id,'specs'=>$post['specid'],'uniacid'=>$_SESSION['uniacid']),'id');
	//默认商品规格价格：
	$goods=pdo_get('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	$goods['optionid']=$optionsid;
	$goods['storeid']=$store_id;
	$goods=$this->model('activity')->get_upgoods($goods);
	$return['options']=$goods['options'];
	$return['options']['thumb']=$return['options']['thumb']?tomedia($return['options']['thumb']):tomedia($goods['image']);
	$parms=array('optionid'=>$return['options']['id'],'goodsid'=>$id,'mid'=> $_SESSION['uid'], 'uniacid' => $_SESSION['uniacid']);
	if($post['isdish']){
		$parms['selected']=1;
	}
	$cart=pdo_get('jy_weishop_member_cart',$parms,array('id','total'));
	$return['carttotal']=$cart['total'];
	$return['cartid']=$cart['id'];
	$return['carttotal']=$return['carttotal']?:0;
	$return['selespename'] = $selespename;
	exi($return);
}
if($_RQ['op']=='change_address'){
	$post=get_postdata();
	$addressid = intval($post['addressid']);
	$id=$_SESSION['goodsid'];
	if(!$id){
		exi('请先选择商品！','error');
	}
	$address =  pdo_get('core_address',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid'],'id'=>$addressid));
	$address['pcd']=json_decode($address['pcd'],true);
	$return['address'] =$address;
	exi($return);
}
if($_RQ['op']=='like'){
	if(!$_SESSION['uid']){
		$return['ischecklogin']=$this->model('member')->checklogin();
	}
	$id = $_SESSION['goodsid'];
	if(!$id){
		$post=get_postdata();
	}
	$like=pdo_get('jy_weishop_member_like',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'goodsid'=>$id),'id');
	if($like){
			pdo_delete('jy_weishop_member_like',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'goodsid'=>$id));
			exi('取消成功');
	}else{
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'goodsid'=>$id,
			'createtime'=>TIMESTAMP
		);
		pdo_insert('jy_weishop_member_like',$insert);
		exi('关注成功');
	}	
}
if($_RQ['op']=='check_buy'){
	$goodsid=intval($_RQ['goodsid'])?:$_SESSION['goodsid'];
	if(!$goodsid){
		exi('请选择商品！','error');
	}
	$goods=pdo_get('jy_weishop_goods',array('id'=>$goodsid),array('id','title','no_onebuy','merchid'));
	if($goods['no_onebuy']==1 || $goods['no_onebuy']==3){
		exi('该商品，不能单独购买！','error');
	}
	if($return['shopset']['no_showmerch']==1 && $goods['merchid']){
		exi('该商品，属于商户商品，请前往商户小店购买！','error');
	}
	$this->model('activity/sales')->check_sale_new(array(array('goodsid'=>$goodsid)));
	$this->model('goods')->check_buy($goodsid,$_RQ['num'],$_RQ['optionsid']);
	if($_SESSION['ocreateurl']){
		$ocreateurl=links($_SESSION['ocreateurl']);
		if($ocreateurl['plugin']!=$_SESSION['current_plugin']){
			unset($_SESSION['ocreateurl']);
		}
	}
	$query='goodsid='.$goodsid.'&num='.$_RQ['num'].'&optionsid='.$_RQ['optionsid'].'&addressid='.$_RQ['addressid'].'&storeid='.$_RQ['storeid'].'&roomid='.$_RQ['roomid'].'&dayid='.$_RQ['dayid'].'&out_time='.$_RQ['out_time'];
	if($_SESSION['current_plugin']=='jy_weishopmeituan' ||  $_SESSION['current_plugin']=='jy_weidish' || $_SESSION['current_plugin']=='jy_weishoppei' || $_SESSION['current_plugin']=='jy_weishoppeishop' || $_SESSION['current_plugin']=='jy_weishopscale'){
		if($_SESSION['current_plugin']=='jy_weishoppeishop'){
			$resurl='jy_weishoppei/create//'.$query;
		}elseif($_SESSION['current_plugin']=='jy_weishopscale'){
			$resurl='jy_weishopscale/create//'.$query;
		}elseif($_SESSION['current_plugin']=='jy_weishopmeituan'){
			$resurl='jy_weishopmeituan/create//'.$query;
		}else{
			$resurl=$_SESSION['current_plugin'].'/create//'.$query;
		}
	}elseif($_SESSION['ocreateurl']){
		$resurl=$_SESSION['ocreateurl'].$query;
	}elseif($goods['merchid']){
		$resurl='jy_merch/order.create//'.$query;
	}elseif($return['shopset']['ordercreateurl']){
		$resurl=$return['shopset']['ordercreateurl'].'/'.$query;
	}else{
		$resurl='jy_weishop/order.create//'.$query;
	}
	if($_RQ['isresurl']){
		exi($resurl);
	}else{
		exi('','',$resurl);
	}
}
if($_RQ['op']=='getcomments'){
	$goodsid=$_RQ['goodsid']?intval($_RQ['goodsid']):$_SESSION['goodsid'];
	$return['list']=$this->model('goods')->get_comments($goodsid,6,$comment_total,$_RQ['type'],$_RQ['page']);
	exi($return);
}
if($_RQ['op']=='getactive'){
	$goods_id=intval($_RQ['goods_id']);
	$active_id=intval($_RQ['active_id']);
	$active_plugin=$_RQ['active_plugin'];
	if($active_plugin && $goods_id){
		$active=$this->model('activity')->get_active($goods_id,$active_id,$active_plugin);
	}
	$return['active']=$active;
	exi($return);
}
if($_RQ['op']=='upposter'){
	$goodsid=intval($_RQ['goodsid']);
	//小程序二维码：
	$url='/jy_weishop/goods/detail/index?id='.$goodsid.'&fromuser='.$_SESSION['uid'];
	$pid='wxappnew'.$_SESSION['wxapp_id'].'_'.$goodsid.'_'.$_SESSION['uid'];
	if($_SESSION['fromh5wxapp']){
		$h5url='jy_weishop/goods.detail//id='.$goodsid.'&fromuser='.$_SESSION['uid'];
		$url='/pages/index/index?url='.urlencode($h5url);
	}
	//海报文件
	if($_SESSION['fromh5wxapp'] || $_SESSION['container']=='wxapp'){
		$appcode = $this->model('model')->get_wxappewm($url,$pid);
		$poster=model('jy_poster/model')->get_active(3,'',$goodsid,$_SESSION['openid'],$appcode,'','jy_weishop','',true);
	}else{
		$poster=model('jy_poster/model')->get_active(3,'',$goodsid,$_SESSION['openid'],'','','jy_weishop','',true);
	}
	$poster['posterimg']=$poster['poster'];
	exi($poster);
}